<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="请购单号" prop="planId">
        <el-input
          v-model="queryParams.planId"
          placeholder="请输入请购单号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rowList">
      <el-table-column label="入库单号" align="center" prop="id" width="120" fixed="left"/>
      <el-table-column label="单据状态" align="center" prop="status" width="150">
        <template slot-scope="scope">
          <span :style="{'color': statusColors(scope.row.status)}">{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检测状态" align="center" prop="examineStatus" width="150">
        <template slot-scope="scope">
          <span @click="handleJumpExamine(scope.row)" :style="{'color': examineColors(scope.row.examineStatus), 'cursor': 'pointer'}">{{ scope.row.examineStatus }}</span>
        </template>
      </el-table-column>
      <el-table-column label="请购单号" align="center" prop="planId" width="180"/>
      <el-table-column label="园区" align="center" prop="area" width="120"/>
      <el-table-column label="确定人" align="center" prop="confirmer" width="150"/>
      <el-table-column label="确定时间" align="center" prop="confirmTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.confirmTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库日期" align="center" prop="inTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="供应商名称" align="center" prop="vendorName" width="180"/>
      <el-table-column label="入库总金额" align="center" prop="amount" width="150"/>
      <el-table-column label="留样照片" prop="images" align="center" :width="imageColWidth">
        <template v-slot:default="scope">
          <image-preview v-for="(item,index) in scope.row.imageArr"
                    style="width: 100px; height: 100px; margin-right: 15px"
                    :src="item">
          </image-preview>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="150"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
        <template v-slot="{row}">
          <el-button v-if="row.status==='待入库' && row.examineStatus==='检测通过'" size="mini" type="text" icon="el-icon-check" @click="sureIn(row)">确认入库</el-button>
          <el-button v-if="row.status==='待入库'" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDetail(row)">明细</el-button>
          <el-button v-if="row.status==='待入库'" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="请购单号" prop="planId">
              <el-select v-model="form.planId"  placeholder="请选择请购单" :disabled="form.id" clearable :style="{width: '100%'}" @change="planIdChange">
                <el-option v-for="(item, index) in planList" :key="index" :label="`${item.name}(${item.planId})`"
                           :value="item.planId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库日期" prop="inTime">
              <el-date-picker
                type="date"
                v-model="form.inTime"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                :style="{ width: '100%' }"
                placeholder="请选择入库日期"
                clearable
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商" prop="remark">
              <el-input :disabled="true" v-model="form.vendorName"   />
<!--              <el-select v-model="form.vendorId" placeholder="请选择供应商" clearable :style="{width: '100%'}">-->
<!--                <el-option v-for="(item, index) in vendorOptions" :key="index" :label="item.label"-->
<!--                          :value="item.value"></el-option>-->
<!--              </el-select>-->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="索证索票" prop="imageUrl" >
          <ImageUpload
            v-model="form.imageUrl"/>
        </el-form-item>
        <el-row>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, get, add, update, del } from "@/api/store/in/in";
import { listAll as listAllVendor } from "@/api/store/vendor";
import {getBuffetPlanList, getNormalPlanList} from "@/api/store/plan/temp";
import moment from "moment/moment";
export default {
  name: "StoreInNormal",
  dicts: [],
  data() {
    return {
      imageColWidth:150,
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      rowList: [],
      // 弹出层标题
      title: "",
      statusOptions: [
        {
          label: "待入库",
          value: "待入库"
        },
        {
          label: "已入库",
          value: "已入库"
        }
      ],
      vendorOptions: [],
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {
        inTime:'',
      },
      // 表单校验
      rules: {
        planId:[{ required: true, trigger: "change", message:'请选择请购单'}],
      },
      planList:[]
    };
  },
  created() {
    // listAllVendor().then(res => {
    //   this.vendorOptions = [];
    //   for (var i = 0; i < res.rows.length; i++) {
    //     this.vendorOptions.push({
    //       label: res.rows[i].name,
    //       value: res.rows[i].id
    //     });
    //   }
    // })
    this.getList();
  },
  methods: {
    //选择请购单号
    planIdChange(data){
      this.form.vendorId=this.planList.find(item=>item.planId===data).vendorId || ''
      this.form.vendorName=this.planList.find(item=>item.planId===data).vendorName || ''
    },
    statusColors(status) {
      if (status === "已入库") {
        return '#2a9c4a'
      } else {
        return '#fe9f29'
      }
    },
    examineColors(status) {
      if (status === "检测通过") {
        return '#2a9c4a'
      } else if (status === "检测不通过") {
        return '#FF0000'
      } else {
        return '#fe9f29'
      }
    },
    handleJumpExamine: function(row) {
      const id = row.examineId;
      this.$router.push("/examine/in/detail/"+id);
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      this.queryParams.type = "自助餐入库单";
      list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        let maxWith = 1;
        response.rows.map(item=>{
          item.imageArr=[]
          if (item.imageUrl ) {
            item.imageArr = item.imageUrl.split(",");
          }
          if (item.imageArr.length > maxWith) {
            maxWith = item.imageArr.length;
          }
        })
        this.imageColWidth = maxWith * 150;
        this.rowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      getBuffetPlanList().then(res=>{
        this.planList=res.data
      })
      this.reset();
      this.statusOptions = [
        {
          label: "待入库",
          value: "待入库"
        }
      ];
      this.form = {
        status: "待入库",
        type: "自助餐入库单",
        inTime:moment().format('yyyy-MM-DD HH:MM:SS'),
      }
      this.open = true;
      this.title = "添加入库单";
      // this.form.inTime=moment().format('yyyy-MM-DD')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      get(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改入库单";
        if (response.data.status === "已入库") {
          this.statusOptions = [
            {
              label: "已入库",
              value: "已入库"
            }
          ];
        } else {
          this.statusOptions = [
            {
              label: "待入库",
              value: "待入库"
            },
            {
              label: "已入库",
              value: "已入库"
            }
          ];
        }
      });
    },
    handleDetail(row) {
      const id = row.id;
      this.$router.push("/store/in/buffet/detail/" + id);
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.type = "自助餐入库单"
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //确认入库
    sureIn(row){
      this.$modal.confirm('请确认是否完成该入库单').then(()=> {
        row.status='已入库'
        update(row).then(response => {
          this.$modal.msgSuccess("入库成功");
          this.getList();
        }).catch(()=>{
          this.getList();
        })
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除单号为"' + row.id + '"的数据项？').then(function() {
        return del(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
